package node;
import func.*;
import java.util.*;

public class FuncNode implements Node, Iterable<Node> {
	
	private Function func;
	private ArrayList<Node> children;
	
	public FuncNode(Function func, ArrayList<Node> children) {
		this.func = func;
		this.children = children;
	}
	
	public int numberOfChildren() {
		return children.size();
	}
	
	public Node getChild(int index) {
		return children.get(index);
	}
	
	public void setChildren(ArrayList<Node> children) {
		this.children = children;
	}
	
	public Iterator<Node> iterator() {
		return children.iterator();
	}
	
	public int evaluate(int[] param) {
		int[] results = new int[children.size()];
		for(int i = 0; i < children.size(); i++)
			results[i] = children.get(i).evaluate(param);
		
		return func.applyFunction(results);
	}
	
	public void display(int indent) {
		String in = new String();
		for(int i = 0; i < indent; i++)
			in = in + " ";
		
		System.out.println(in + func);
		for(Node child : children)
			child.display(indent + 1);
	}
	
	public Node copyNode() {
		ArrayList<Node> copyChildren = new ArrayList<Node>(3);
		for(Node child : children)
			copyChildren.add(child.copyNode());
		
		return new FuncNode(func, copyChildren);
	}
}
